var idleTime = 0;
loadCtr = 0;
jQuery(document).ready(function(){ 

	document.cookie = '__atuvc=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
	document.cookie = '__atuvs=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
	document.cookie = '_pk_id.20.db4a=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
	document.cookie = '_pk_ses.20.db4a=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
	document.cookie = 'CYOID=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
	
	//Increment the idle time counter every minute.
	//var idleInterval = setInterval(timerIncrement, 60000); // 1min.
    var idleInterval = setInterval(timerIncrement, 1800000); // 30mins.

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e) {
        idleTime = 0;
    });

    ua_tab2ics = "IMM76D";
	_ua = navigator.userAgent;
	if(_ua.search(ua_tab2ics) > 0){
	//do nothing
	}
	else{
	//Redirect to home page
	/*var expiration = getCookie("tiangge_expiration");
	if(expiration=="") 
      window.location= baseurl + "welcome";*/
	} 

window.onload = function() { 	
	
	window.postMessage(document.body.scrollHeight,"*");
	
	jQuery( window ).resize( function () {
	        //console.log("onload resize-" + document.body.scrollHeight);
			var height = document.body.scrollHeight;
			
			
			if ("window", $( window ).width() ==  1024){
				//do nothing
			}else {
				if (/gallery/.test(self.location.href)){ 
					var hgt = jQuery("div.category-products").height();
					if (hgt > 370){
					    //console.log("onload if " + height);
						window.parent.postMessage(parseInt(height),"*");
					}
				} else if (/thankyou/.test(self.location.href) || /landing/.test(self.location.href) || /gadgetdetails/.test(self.location.href) || /prepaid/.test(self.location.href) || /promo/.test(self.location.href) || /userprofile/.test(self.location.href) || /summary/.test(self.location.href)){
					if (ie_ver()==11){
						//do nothing
					}
				} else { //console.log("onload else " + height);
					if(("window", $( window ).width() == 480) && (/iPhone/.test(navigator.userAgent)) && (/specification/.test(self.location.href))){
						height = 1965;
					}
					window.parent.postMessage(parseInt(height),"*");
				}
			}
				
			
	}).resize();	// on change
	
	jQuery('html,body').bind('heightChange', function(){ });
}

        /**
         * Disable right click
         */
		 
        jQuery(document).bind("contextmenu",function(e){
           //return false;
           e.preventDefault();
        });
});
function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime > 29) { // 30 minutes
		//alert("Logout");
		deleteCookies();
        window.location= baseurl + "welcome";
    }
}
// onload resize
function resizeIframe() {
	preventdefault = false;
	//setTimeout(
		//function(){
			//window.parent.postMessage(document.body.scrollHeight,"*");
			/*var body = document.body,
			html = document.documentElement;
			//var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
			 height = html.offsetHeight;
			
			var ua = window.navigator.userAgent;
			var msie = ua.indexOf("MSIE");
			if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
				height = body.scrollHeight;
			} */
			
			// height should be smaller if in gallery and mobile
			if (/landing/.test(self.location.href)){
				//height = jQuery("#phone-gallery #phoneGallery").height
				//console.log("landing");
				 note2 = "GT-N7105";
				 tabwhite = navigator.userAgent.indexOf("Mozilla/5.0 (Linux; U; Android 4.1.1; en-ph; GT-P3110 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30") != -1;
	
				_ua = navigator.userAgent;
				if(_ua.search(note2) > 0){
				 //alert("note2"); 2406
				}
				if(tabwhite){
					//alert("tabwhite"); 
				}
					height = jQuery("#footerdetect").css({"display":"block"}).position().top;
					/* if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ 
						var ieversion=new Number(RegExp.$1) 
						if (ieversion>=8) 
						height = jQuery("#footerdetect").css({"display":"block"}).position().top;
						//console.log("IE 8");
					} */
			} else if (/thankyou/.test(self.location.href)){
				//height = jQuery("#phone-gallery #phoneGallery").height();
				height = jQuery("#footerdetect").css({"display":"block"}).position().top;
			} else if (/compare/.test(self.location.href)){
				//height = jQuery(".comparisonPage").height();
				height = jQuery("#footerdetect").css({"display":"block"}).position().top;
			} else if (/specification/.test(self.location.href)){ 
				//height = jQuery(".comparisonPage").height();
				if ("window", $( window ).width() == 1024)
				{
				    height = document.body.scrollHeight;
					
					ua_tab2ics = "IMM76D";
					ua_tab2icswhite = "GT-P3110";
					_ua = navigator.userAgent;
					if (/IMM76D/.test(_ua) || /GT-P3110/.test(_ua)){ 
						height = 1770;
					}	
			    } 
				
				else if("window", $( window ).width() == 480){
					height = 1965;
					_ua = navigator.userAgent;
					if (/iPhone/.test(_ua)){ 
						height = 1960;
						jQuery(".lavaL").hide();
					}
				}
				
				else {
				    height = jQuery("#footerdetect").css({"display":"block"}).position().top;
				}
			} else if (/prepaid/.test(self.location.href)){
				//height = jQuery("#phone-gallery #phoneGallery").height
				//console.log("welcome");
				height = jQuery("#footerdetect").css({"display":"block"}).position().top + 46;
				if (ie_ver()==11){
					height = jQuery("#footerdetect").css({"display":"block"}).position().top;
				}
			} else if (/userprofile/.test(self.location.href)){
				//height = jQuery("#phone-gallery #phoneGallery").height
				//console.log("welcome");
				height = jQuery("#footerdetect").css({"display":"block"}).position().top;
				if (ie_ver()==11){
					height = jQuery("#footerdetect").css({"display":"block"}).position().top;
				}
			} else if (/globestore/.test(self.location.href)){ 
				if ("window", $( window ).width() == 1024) {
				    height = document.body.scrollHeight;
			    } else {
				    height = jQuery("#footerdetect").css({"display":"block"}).position().top;
				}
			}
			else {
				height = jQuery("#footerdetect").css({"display":"block"}).position().top;
			}
			//console.log("body.scrollHeight: " + body.scrollHeight + " - body.offsetHeight: " + body.offsetHeight + " - html.clientHeight: " + html.clientHeight + " - html.scrollHeight:" + html.scrollHeight + " - html.offsetHeight:" + html.offsetHeight);
			
			/* if ((navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("ipad") != -1) ) {
				height = document.body.scrollHeight;
				//alert("FIX FOR ipad" + height);
			} */
			
			//console.log("resize height: " + parseInt(height));
			if (ie_ver()==11){
				console.log("This is for testing purposes. Please disregard. HEIGHT: " + height);
			}
			//alert("resize " + parseInt(height));
			window.parent.postMessage(parseInt(height),"*");
			if(preventdefault) {
				//alert("height: " + height + " loadCtr: " + loadCtr);
				loadCtr++;
				preventdefault = false;
				return false;
			}
		//},d 2000); 
} 
function scrollFrameHeight(heightVar) { window.parent.postMessage("scroll-"+heightVar, '*'); }

function setCookie(cname,cvalue) {
    var d = new Date();
    d.setTime(d.getTime() + (30*60*1000));
	
	ua_tab2ics = "IMM76D";
	_ua = navigator.userAgent;
	
	if(_ua.search(ua_tab2ics) > 0)
	    document.cookie = cname+"="+cvalue+"; path=/"; 
	else{
	
	var expiration = getCookie("tiangge_expiration");
	if(expiration=="")
		var expires = "expires=" + d.toUTCString();
	else{
		var e = new Date();
		e.setTime(parseInt(expiration) + (30*60*1000));
	    var expires = "expires=" + e.toUTCString();
	}
	
	//document.cookie = cname+"="+cvalue+"; path=/;"+expires;
	document.cookie = cname+"="+cvalue+"; path=/";
	}
}

   //Retrieve Cookie without space
function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) 
	{
	var c = ca[i].replace(/\s/g, "");
	if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
}
	//Retrieve Cookie with space
function getCookieR(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
   
 function deleteCookies() {
		document.cookie = 'address=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'area=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'bday=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'choosePromo=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'commitmentBox=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'commitmentMonths=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'current_pos=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'discounted=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'eadd=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'gadget=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'gadgetid=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'globestore=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'globestoreid=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'gadgetid=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'imgsrc=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'mobtel=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'months=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'nname=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'othercont=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'postalcode=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'postalIds=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'price=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'promo=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'promoDesc=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'promoprice=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'province=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'rebateamt=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'sku=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'sumname=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'totalcash=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'totalrebate=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'town=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'username=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'validity=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'temp_mobtel=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'mode=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'usertype=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'storehours=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'storeLong=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'storeLat=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'planprice=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'loc=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'globestoreAddress=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'globestoreAddress=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'mt_client=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'expiration=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
		document.cookie = 'region=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
}
   
function ie_ver(){  
    var iev=0;
    var ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
    var trident = !!navigator.userAgent.match(/Trident\/7.0/);
    var rv=navigator.userAgent.indexOf("rv:11.0");

    if (ieold) iev=new Number(RegExp.$1);
    if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
    if (trident&&rv!=-1) iev=11;

    return iev;         
}   